1 /*
2  * Copyright (c) 2011-2012 - Mauro Carvalho Chehab
3  * Copyright (c) 2012 - Andre Roth <neolynx@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation version 2.1 of the License.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  * Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
18  *
19  */
20 
21 /**
22  * @file eit.h
23  * @ingroup dvb_table
24  * @brief Provides the table parser for the DVB EIT (Event Information Table)
25  * @copyright GNU Lesser General Public License version 2.1 (LGPLv2.1)
26  * @author Mauro Carvalho Chehab
27  * @author Andre Roth
28  *
29  * @par Relevant specs
30  * The table described herein is defined at:
31  * - ETSI EN 300 468
32  *
33  * @see
34  * http://www.etherguidesystems.com/Help/SDOs/dvb/syntax/tablesections/EIT.aspx
35  *
36  * @par Bug Report
37  * Please submit bug reports and patches to linux-media@vger.kernel.org
38  */
39 
40 module libdvbv5_d.eit;
41 
42 import core.stdc.time: tm;
43 import core.sys.posix.unistd;
44 
45 import libdvbv5_d.descriptors: dvb_desc;
46 import libdvbv5_d.dvb_fe: dvb_v5_fe_parms;
47 import libdvbv5_d.header: dvb_table_header;
48 
49 extern (C):
50 
51 /* ssize_t */
52 
53 /**
54  * @def DVB_TABLE_EIT
55  *	@brief DVB EIT table ID for the actual TS
56  *	@ingroup dvb_table
57  * @def DVB_TABLE_EIT_OTHER
58  *	@brief DVB EIT table ID for other TS
59  *	@ingroup dvb_table
60  * @def DVB_TABLE_EIT_PID
61  *	@brief DVB EIT Program ID
62  *	@ingroup dvb_table
63  * @def DVB_TABLE_EIT_SCHEDULE
64  *	@brief Start table ID for the DVB EIT schedule data on the actual TS
65  *		The range has 0x0f elements (0x50 to 0x5F).
66  *	@ingroup dvb_table
67  * @def DVB_TABLE_EIT_SCHEDULE_OTHER
68  *	@brief Start table ID for the DVB EIT schedule data on other TS
69  *		The range has 0x0f elements (0x60 to 0x6F).
70  *	@ingroup dvb_table
71  */
72 enum DVB_TABLE_EIT = 0x4E;
73 enum DVB_TABLE_EIT_OTHER = 0x4F;
74 enum DVB_TABLE_EIT_PID = 0x12;
75 
76 enum DVB_TABLE_EIT_SCHEDULE = 0x50;
77 enum DVB_TABLE_EIT_SCHEDULE_OTHER = 0x60;
78 
79 /**
80  * @struct dvb_table_eit_event
81  * @brief DVB EIT event table
82  * @ingroup dvb_table
83  *
84  * @param event_id		an uniquelly (inside a service ID) event ID
85  * @param desc_length		descriptor's length
86  * @param free_CA_mode		free CA mode. 0 indicates that the event
87  *				is not scrambled
88  * @param running_status	running status of the event. The status can
89  *				be translated to string via
90  *				dvb_eit_running_status_name string table.
91  * @param descriptor		pointer to struct dvb_desc
92  * @param next			pointer to struct dvb_table_eit_event
93  * @param tm_start		event start (in struct tm format)
94  * @param duration		duration in seconds
95  * @param service_id		service ID
96  *
97  * This structure is used to store the original EIT event table,
98  * converting the integer fields to the CPU endianness, and converting the
99  * timestamps to a way that it is better handled on Linux.
100  *
101  * The undocumented parameters are used only internally by the API and/or
102  * are fields that are reserved. They shouldn't be used, as they may change
103  * on future API releases.
104  *
105  * Everything after dvb_table_eit_event::descriptor (including it) won't
106  * be bit-mapped to the data parsed from the MPEG TS. So, metadata are added
107  * there.
108  */
109 struct dvb_table_eit_event
110 {
111     align (1):
112 
113     ushort event_id;
114 
115     union
116     {
117         align (1):
118 
119         ushort bitfield1; /* first 2 bytes are MJD, they need to be bswapped */
120         ubyte[5] dvbstart;
121     }
122 
123     ubyte[3] dvbduration;
124 
125     union
126     {
127         align (1):
128 
129         ushort bitfield2;
130 
131         struct
132         {
133             import std.bitmanip : bitfields;
134             align (1):
135 
136             mixin(bitfields!(
137                 ushort, "desc_length", 12,
138                 ushort, "free_CA_mode", 1,
139                 ushort, "running_status", 3));
140         }
141     }
142 
143     // struct dvb_desc;
144     dvb_desc* descriptor;
145     dvb_table_eit_event* next;
146     tm start;
147     uint duration;
148     ushort service_id;
149 }
150 
151 /**
152  * @struct dvb_table_eit
153  * @brief DVB EIT table
154  * @ingroup dvb_table
155  *
156  * @param header	struct dvb_table_header content
157  * @param transport_id	transport id
158  * @param network_id	network id
159  * @param last_segment	last segment
160  * @param last_table_id	last table id
161  * @param event		pointer to struct dvb_table_eit_event
162  *
163  * This structure is used to store the original EIT table,
164  * converting the integer fields to the CPU endianness.
165  *
166  * Everything after dvb_table_eit::event (including it) won't
167  * be bit-mapped to the data parsed from the MPEG TS. So, metadata are added
168  * there.
169  */
170 struct dvb_table_eit
171 {
172     align (1):
173 
174     dvb_table_header header;
175     ushort transport_id;
176     ushort network_id;
177     ubyte last_segment;
178     ubyte last_table_id;
179     dvb_table_eit_event* event;
180 }
181 
182 /**
183  * @brief Macro used to find event on a DVB EIT table
184  * @ingroup dvb_table
185  *
186  * @param _event	event to seek
187  * @param _eit		pointer to struct dvb_table_eit_event
188  */
189 
190 // struct dvb_v5_fe_parms;
191 
192 /** @brief Converts a running_status field into string */
193 extern __gshared const(char)*[8] dvb_eit_running_status_name;
194 
195 /**
196  * @brief Initializes and parses EIT table
197  * @ingroup dvb_table
198  *
199  * @param parms	struct dvb_v5_fe_parms pointer to the opened device
200  * @param buf buffer containing the EIT raw data
201  * @param buflen length of the buffer
202  * @param table pointer to struct dvb_table_eit to be allocated and filled
203  *
204  * This function allocates an EIT table and fills the fields inside
205  * the struct. It also makes sure that all fields will follow the CPU
206  * endianness. Due to that, the content of the buffer may change.
207  *
208  * @return On success, it returns the size of the allocated struct.
209  *	   A negative value indicates an error.
210  */
211 ssize_t dvb_table_eit_init (
212     dvb_v5_fe_parms* parms,
213     const(ubyte)* buf,
214     ssize_t buflen,
215     dvb_table_eit** table);
216 
217 /**
218  * @brief Frees all data allocated by the DVB EIT table parser
219  * @ingroup dvb_table
220  *
221  * @param table pointer to struct dvb_table_eit to be freed
222  */
223 void dvb_table_eit_free (dvb_table_eit* table);
224 
225 /**
226  * @brief Prints the content of the DVB EIT table
227  * @ingroup dvb_table
228  *
229  * @param parms	struct dvb_v5_fe_parms pointer to the opened device
230  * @param table pointer to struct dvb_table_eit
231  */
232 void dvb_table_eit_print (dvb_v5_fe_parms* parms, dvb_table_eit* table);
233 
234 /**
235  * @brief Converts a DVB EIT formatted timestamp into struct tm
236  * @ingroup dvb_table
237  *
238  * @param data		event on DVB EIT time format
239  * @param tm		pointer to struct tm where the converted timestamp will
240  *			be stored.
241  */
242 void dvb_time (ref const(ubyte)[5] data, tm* tm);